<?php
class Admin_KhoaHocController extends Zend_Controller_Action {
	public function init(){
		$this->_arrParam = $this->_request->getParams();
		$this->view->arrParam = $this->_arrParam;
	}
	public function indexAction()
	{
		$dkkhMapper = Data_Model_KhoaHocMapper::getInstance();
		$dkkhs = $dkkhMapper->khoahocList();
		if($this->getRequest()->isPost())
		{
			foreach($dkkhs as $dkkh)
			{
				$id = $dkkh->getId();
				$chon = $this->_arrParam['case'.$id];
				if($chon == '1')
				{
					$dkkhMapper->delete($id);
				}
			}
			$this->_redirect('/admin/khoahoc/index');
		}
		$this->view->dkkh = $dkkhs;
		
	}
	
	public function danhsachAction()
	{
		$id = $this->_request->getParam('id');
		$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
		$danhsach = $khoahocMapper->searchById($id);
		$this->view->danhsach = $danhsach;
	}
	public function themAction()
	{
		$this->view->title = "Thêm Khóa Học";
		$formThem = new Admin_Form_KhoaHocForm();
		$formThem->setMethod('post');
		$formThem->setAction('/QLHV/public/admin/khoahoc/them/');
		
		$this->view->form = $formThem;	
		if($this->getRequest()->isPost())
		{
			if($formThem->isValid($_POST))
			{
				$data = $formThem->getValues();
		
				$makhoahoc			= $data['makhoahoc'];
				$tenkhoahoc			= $data['tenkhoahoc'];
				$malophoc			= $data['malophoc'];
				$ngaykhaigiang		= $data['ngaykhaigiang'];
				$ngaythi			= $data['ngaythi'];
				$ghichu				= $data['ghichu'];
				$magiaovien			= $data['magiaovien'];
				$trangthai			= $data['trangthai'];
				$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
				$kythiMapper = Data_Model_KyThiMapper::getInstance();
				$khoahocObj = new Data_Model_KhoaHoc();
				$kythiObj  = new Data_Model_KyThi();
		
				$khoahocObj->setMakhoahoc($makhoahoc);
				$khoahocObj->setTenkhoahoc($tenkhoahoc);
				$khoahocObj->setMalop($malophoc);
				$khoahocObj->setNgaykhaigiang($ngaykhaigiang);
				$khoahocObj->setMagiaovien($magiaovien);
				$khoahocObj->setTrangthai($trangthai);
		
				$khoahoc = $khoahocMapper->save($khoahocObj);
				
				$kythiObj->setMakhoahoc($khoahoc);
				$kythiObj->setNgaythi($ngaythi);
				$kythiObj->setGhichu($ghichu);
				
				$kythi = $kythiMapper->save($kythiObj);
		
				$this->_redirect('/admin/khoahoc/index');
		
			}
		}
		
	}
	public function capnhatAction()
	{
		$formCapnhat = new Admin_Form_KhoaHocForm();
		$this->view->title = "Cập nhật Khóa học";
		$formCapnhat->setMethod('post');
		$formCapnhat->setAction('/QLHV/public/admin/khoahoc/capnhat/');
		$formCapnhat->addElement('hidden','id');
		$formCapnhat->addElement('submit','submit',array('label'=>'Lưu'));
		$id = $this->getRequest()->getParam('id');
		$formCapnhat->getElement('tenkhoahoc')->addValidator('Db_NoRecordExists',true, array(
				'table' => 'tbl_khoahoc',
				'field' => 'TenKhoaHoc',
				'exclude' => array(
						'field' => 'MaKhoaHoc',
						'value' => $id)
		));
		$formCapnhat->getElement('tenkhoahoc')->getValidator('Db_NoRecordExists')->setMessage('Khóa học đã tồn tại!');
		$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
		$this->view->form = $formCapnhat;
	
		if($this->getRequest()->isPost())
		{
			if($formCapnhat->isValid($_POST))
			{
				$data = $formCapnhat->getValues();
	
				$id 				= $data['id'];
				$makhoahoc			= $data['makhoahoc'];
				$tenkhoahoc			= $data['tenkhoahoc'];
				$malophoc			= $data['malophoc'];
				$ngaykhaigiang		= $data['ngaykhaigiang'];
				$magiaovien			= $data['magiaovien'];
				$trangthai			= $data['trangthai'];
				$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
				$khoahocObj = new Data_Model_KhoaHoc();
		
				$khoahocObj->setId($id);
				$khoahocObj->setMakhoahoc($makhoahoc);
				$khoahocObj->setTenkhoahoc($tenkhoahoc);
				$khoahocObj->setMalop($malophoc);
				$khoahocObj->setNgaykhaigiang($ngaykhaigiang);
				$khoahocObj->setMagiaovien($magiaovien);
				$khoahocObj->setTrangthai($trangthai);
		
				$khoahoc = $khoahocMapper->save($khoahocObj);
		
				$this->_redirect('/admin/khoahoc/index');
			}
		}
	
		else {
			
			$khoahoc = $khoahocMapper->searchById($id);
	
			$formCapnhat->getElement('id')->setValue($khoahoc->getId());
			$formCapnhat->getElement('makhoahoc')->setValue($khoahoc->getMakhoahoc());
			$formCapnhat->getElement('tenkhoahoc')->setValue($khoahoc->getTenkhoahoc());
			$formCapnhat->getElement('malophoc')->setValue($khoahoc->getMalop());
			$formCapnhat->getElement('ngaykhaigiang')->setValue($khoahoc->getNgaykhaigiang());
			$formCapnhat->getElement('magiaovien')->setValue($khoahoc->getMagiaovien());
			$formCapnhat->getElement('trangthai')->setValue($khoahoc->getTrangthai());
		}
	}
	
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$makhoahoc = $this->getRequest()->getParam('id');
		$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
		$khoahocMapper->delete($makhoahoc);
		$this->_redirect('/admin/khoahoc/index');
	}
	
	public function validateformAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$form = new Admin_Form_KhoaHocForm();
		$form->isValid($this->_getAllParams());
		$json = $form->getMessages();
		header('Conten-type: application/json');
		echo Zend_Json::encode($json);
	
	}
}

?>